﻿using System;
//using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.PocketOutlook;
using Google.GData.Calendar;
using Google.GData.Extensions;
using System.Drawing;
using System.Windows.Forms;

namespace Synchro
{
    public class Synchro
    {
        private Google _google=null;
        private Outlook _outlook=null;

        public Synchro() {
            _google = Google.getGoogle();
            _outlook = Outlook.getOutlook();
        }

        public Google google
        {
            get { return _google; }
            set { _google = value; }
        }

        public Outlook outlook
        {
            get { return _outlook; }
            set { _outlook = value; }
        }

        public int synchronize(int option, int index,ProgressBar progress) {
            if (option == 0)
            {
                progress.Value = 0;
                int _resultGoogle = fromGoogleToOutlook(index, progress);
                if (_resultGoogle < 0)
                {
                    progress.Value = 100;
                    return _resultGoogle;
                }
                else
                {
                    progress.Value = 50;
                    int _resultOutlook = fromOutlookToGoogle(index, progress);
                    progress.Value = 100;
                    if (_resultOutlook < 0)
                    {
                        return _resultOutlook;
                    }
                    return (_resultGoogle + _resultOutlook);
                }
            }
            else if (option == 1)
            {
                progress.Value = 0;
                int _resultGoogle = fromGoogleToOutlook(index, progress);
                progress.Value = 100;
                return _resultGoogle;
            }
            else
            {//option = 2
                progress.Value = 0;
                int _resultOutlook = fromOutlookToGoogle(index, progress);
                progress.Value = 100;
                return _resultOutlook;
            }
            
        }

        private int fromGoogleToOutlook(int index,ProgressBar progress) {
            int count=0;
            try
            {
                if ((_google != null) && (_google.isConnected()))
                {
                    EventFeed google_events = _google.getAllEntries(index);
                    AppointmentCollection outlook_events = _outlook.getAppointments();
                    for (int i = 0; i < google_events.Entries.Count; i++) {
                        EventEntry google_entry = (EventEntry)google_events.Entries[i];

                        /*if (!google_entry.Times[0].AllDay)
                        {
                            google_entry.Times[0].StartTime = google_entry.Times[0].StartTime.AddHours(1);
                            google_entry.Times[0].EndTime = google_entry.Times[0].EndTime.AddHours(1);
                        }*/
                        if (!_outlook.appointmentExistsInOutlook(google_entry, outlook_events))
                        {
                            if (!_outlook.addAppointment(google_entry.Title.Text, google_entry.Times[0].StartTime, google_entry.Times[0].EndTime,
                                                        google_entry.Times[0].AllDay, google_entry.Locations[0].ValueString))
                            {
                                return -2;
                            }
                            else {
                                count++;
                            }
                        }
                        progress.Value += 50 / google_events.Entries.Count;
                    }
                }
                else {
                    return -500;//Not logged
                }
            }
            catch {
                return -2;
            }
            return count;
        }

        private int fromOutlookToGoogle(int index,ProgressBar progress) {
            int count = 0;
            try
            {
                AppointmentCollection outlook_events = _outlook.getAppointments();
                EventFeed google_events = _google.getAllEntries(index);
                for (int i = 0; i < outlook_events.Count; i++) {
                    if (!_google.appointmentExistsInGoogle(outlook_events[i], google_events))
                    {
                        EventEntry entry = new EventEntry();

                        // Set the title and content of the entry.
                        entry.Title.Text = outlook_events[i].Subject;
                        entry.Content.Content = outlook_events[i].Body;

                        // Set a location for the event.
                        Where eventLocation = new Where();
                        eventLocation.ValueString = outlook_events[i].Location;
                        entry.Locations.Add(eventLocation);
                        When eventTime;
                        eventTime = new When(outlook_events[i].Start, outlook_events[i].End, outlook_events[i].AllDayEvent);
                        /*if (outlook_events[i].AllDayEvent)
                        {
                            eventTime = new When(outlook_events[i].Start, outlook_events[i].End, outlook_events[i].AllDayEvent);
                        }
                        else
                        {
                            eventTime = new When(outlook_events[i].Start.AddHours(-1), outlook_events[i].End.AddHours(-1), outlook_events[i].AllDayEvent);
                        }*/
                        entry.Times.Add(eventTime);

                        Uri postUri = new Uri(_google.CalendarUri);

                        // Send the request and receive the response:
                        if (_google.insertEntry(entry) != 0)
                        {
                            //Error
                            return -1;
                        }
                        else {
                            count++;
                        }
                    }
                    progress.Value += 50 / outlook_events.Count;
                }
            }
            catch
            {
                return -1;
            }
            return count;
        }

        public bool isConnectedInternet() {
            System.Net.HttpWebRequest req;
            System.Net.HttpWebResponse res;

            try {
                req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(@"http://www.google.com");
                res = (System.Net.HttpWebResponse)req.GetResponse();

                req.Abort();

                if (res.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
